package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.LinearGradient;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeOption;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.PlanNode;
import com.google.gson.Gson;
import com.szgr.baoyin.MainActivity;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.model.ServerInfo;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.RLog;
import com.szgr.baoyin.utils.ToastUtils;
import com.szgr.baoyin.utils.ViewFindUtils;
import com.szgr.baoyin.volley.Volley;
import com.szgr.baoyin.volley.VolleyInterface;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 服务信息
 */
public class ProtectDetailActivity extends BaseActivity implements VolleyInterface {

    private String maintenance_id;
    private ServerInfo serverInfo;
    private ServerInfo.MaintenanceTechnicianInfoEntity server;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constant.REQUEST_SUCCESS:
                    try {
                        JSONObject technicianInfo = new JSONObject(new Gson().toJson(serverInfo.getMaintenance_technician_info()));
                        if (technicianInfo.has("member_truename")) {
                            server = new Gson().fromJson(technicianInfo.toString(), ServerInfo.MaintenanceTechnicianInfoEntity.class);
                            String member_truename = server.getMember_truename();
                            if (!TextUtils.isEmpty(member_truename)) {
                                tv_state.setText("已接单");
                                tv_state.setBackgroundColor(0xff00ff00);
                                tv_server.setText(member_truename);
                                tv_serverNum.setText(server.getMember_id());
                                tv_serverTel.setText(server.getMember_tel());
                                String maintenance_type = serverInfo.getMaintenance_type();
                                if ("01".equals(maintenance_type)) {
                                    tv_serverType.setText("预约");
                                } else {
                                    tv_serverType.setText("急修");
                                }
                                tv_other.setText(server.getMember_desc());
                                tv_time.setText("服务人员正拼命赶过来");
                                String member_position = server.getMember_position();
                                if (member_position != null && member_position.contains(",")) {
                                    String[] split = member_position.split(",");
                                    geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(new LatLng(Double.parseDouble(split[1]), Double.parseDouble(split[0]))));
                                }
                            } else {
//                                tv_state.setText("未接单");
//                                tv_state.setBackgroundColor(0xffff0000);
                                hasMsg.setVisibility(View.GONE);
                                noMsg.setVisibility(View.VISIBLE);
                            }
                        } else {
//                            tv_state.setText("未接单");
//                            tv_state.setBackgroundColor(0xffff0000);
                            hasMsg.setVisibility(View.GONE);
                            noMsg.setVisibility(View.VISIBLE);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };
    private TextView tv_state;
    private TextView tv_server;
    private TextView tv_serverNum;
    private TextView tv_serverTel;
    private TextView tv_serverType;
    private TextView tv_other;
    private TextView tv_location;
    private TextView tv_time;
    private GeoCoder geoCoder;
    private LinearLayout hasMsg;
    private RelativeLayout noMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_protect_detail);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("维保详情");
    }

    @Override
    public void initView() {
        hasMsg = ViewFindUtils.find(view, R.id.has_msg);
        noMsg = ViewFindUtils.find(view, R.id.no_msg);
        findViewById(R.id.protect_detail_judge).setOnClickListener(this);
        findViewById(R.id.protect_detail_complete).setOnClickListener(this);

        tv_state = ViewFindUtils.find(view, R.id.protect_state);
        tv_server = ViewFindUtils.find(view, R.id.protect_person);
        tv_serverNum = ViewFindUtils.find(view, R.id.protect_num);
        tv_serverTel = ViewFindUtils.find(view, R.id.protect_tel);
        tv_serverType = ViewFindUtils.find(view, R.id.protect_type);
        tv_other = ViewFindUtils.find(view, R.id.protect_other);
        tv_location = ViewFindUtils.find(view, R.id.protect_address);
        tv_location.setOnClickListener(this);
        tv_time = ViewFindUtils.find(view, R.id.protect_time);

        View view1 = ViewFindUtils.find(view, R.id.connect_station);
        View view2 = ViewFindUtils.find(view, R.id.connect_kefu);
        view1.setOnClickListener(this);
        view2.setOnClickListener(this);

        //实例化一个地理编码查询对象
        geoCoder = GeoCoder.newInstance();
        geoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
                //获取点击的坐标地址
                System.out.println("----address---->" + result.getLocation().longitude + "," + result.getLocation().latitude);
                if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                    ToastUtils.showToast("抱歉，未能找到结果");
                    return;
                }
                if (result.error == SearchResult.ERRORNO.NO_ERROR) {
                    tv_location.setText(result.getAddress() + "(点击查看地图)");
                } else {
                    ToastUtils.showToast("用户位置信息有误");
                }
            }

            @Override
            public void onGetGeoCodeResult(GeoCodeResult result) {

            }
        });

        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("maintenance_id")) {
            maintenance_id = intent.getStringExtra("maintenance_id");
            Map<String, String> map = new HashMap<>();
            map.put("key", MyApplication.getmInstance().getKey());
            map.put("id", maintenance_id);
            Volley.post(Constant.URL_PROTECT_INFO, map, "info", this);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.protect_detail_judge:
                if (server != null) {
                    String member_truename = server.getMember_truename();
                    if (member_truename != null && !"".equals(member_truename)) {
                        String flag = serverInfo.getFlag();
                        if ("02".equals(flag)) {
                            Intent evaluateIntent = new Intent(this, EvaluateActivity.class);
                            evaluateIntent.putExtra("maintenance_id", maintenance_id);
                            evaluateIntent.putExtra("num", server.getMember_id());
                            evaluateIntent.putExtra("name", member_truename);
                            startActivity(evaluateIntent);
                        } else {
                            ToastUtils.showToast("订单未完成");
                        }
                    } else {
                        ToastUtils.showToast("未接单");
                    }
                }
                break;
            case R.id.protect_detail_complete:
                Intent intent = new Intent(this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
            case R.id.protect_address:
                if (server != null) {

                    String member_position = server.getMember_position();
                    if (member_position != null && member_position.contains(",")) {
                        Intent intent1 = new Intent(this, OrderMapActivity.class);
                        intent1.putExtra("id", server.getMember_id());
                        startActivity(intent1);
                    }
                }
                break;
            case R.id.connect_station:
                if (server != null) {
                    String telPhone = server.getMember_tel();
                    Intent intent2 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + telPhone));
                    intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent2);
                }
                break;
            case R.id.connect_kefu:
                Intent intent1 = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + Constant.PHONE));
                intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent1);
                break;
        }
    }

    @Override
    public void onSuccess(String s, String tag) {
        RLog.s("----detail---->" + s);
        try {
            JSONObject jsonObject = new JSONObject(s);
            JSONObject datas = jsonObject.getJSONObject("datas");
            JSONObject maintenance_info = datas.getJSONObject("maintenance_info");
            Gson gson = new Gson();
            serverInfo = gson.fromJson(maintenance_info.toString(), ServerInfo.class);
            mHandler.obtainMessage(Constant.REQUEST_SUCCESS).sendToTarget();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailed(String tag) {

    }
}
